package com.chuangjiangx.member.manager.web.web.stored.controller;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.domain.shared.model.PayTerminal;
import com.chuangjiangx.member.basic.ddd.query.MerchantUserInfoQuery;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.manager.web.web.stored.request.MbrRefundRequest;
import com.chuangjiangx.member.stored.ddd.application.MbrRefundApplication;
import com.chuangjiangx.member.stored.ddd.application.command.MbrRefundCommand;
import com.chuangjiangx.member.stored.ddd.application.dto.MbrRefundResult;
import com.sun.jmx.snmp.ThreadContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * @author cj-ch
 * @date 2018/6/28 下午6:01
 */
@RestController
@RequestMapping(value = "/api/mbr/refund",produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j(topic = "pay")
public class MbrRefundController extends BaseController {


    @Autowired
    private MbrRefundApplication mbrRefundApplication;
    @Autowired
    private MerchantUserInfoQuery merchantUserInfoQuery;

    @RequestMapping(value = "")
    @Login
    public Response refund(
            @RequestBody @Validated MbrRefundRequest refund,
            HttpSession session){
        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        MbrRefundCommand command = buildRefundCommand(user, refund);
        MbrRefundResult result = mbrRefundApplication.refund(command);
        return ResponseUtils.successCamel(result);
    }


    private MbrRefundCommand buildRefundCommand(ThreadLocalUser user,MbrRefundRequest refund){
        String userPassword = merchantUserInfoQuery.queryUserPassword(user.getMerchantUserId());
        if(!Objects.equals(userPassword,DigestUtils.md5DigestAsHex(refund.getPassword().getBytes()))){
            throw new BaseException("","密码错误");
        }
        MbrRefundCommand command = new MbrRefundCommand();
        command.setMerchantId(user.getMerchantId());
        command.setMerchantUserId(user.getMerchantUserId());
        command.setUserType(user.getUserType());

        command.setOrderNumber(refund.getOrderNumber());
        command.setAmount(refund.getAmount());
        command.setTerminal(PayTerminal.getPayTerminal(refund.getTerminal()));


        /**
         * @see StoreUser.UserType
         */
        switch (user.getUserType()){
            case 0:
                command.setMerchantAddress(user.getMerchantAddress());
                break;
            case 1:
            case 2:
                command.setStoreId(user.getStoreId());
                command.setStoreUserId(user.getStoreUserId());
                command.setStoreAddress(user.getStoreAddress());

                break;
            default:
                break;
        }
        return command;
    }
}

